﻿import random
from locust import HttpUser, task, between
from locust import events

headers = {'Content-Type': 'application/json;charset=utf-8'}
def my_assert(response):
    try:
        if response.status_code != 200:
            response.failure("返回状态码为【%s】，返回报文【%s】，返回response headers为【%s】" % (response.status_code, response.text, response.headers))
        else:
            json_resp = response.json()
            if json_resp["code"] != 1000:
                response.failure("返回code为【%s】与预期值1000不符，返回报文【%s】，返回response headers为【%s】" % (json_resp["code"], response.text, response.headers))
            elif json_resp["msg"] != 'success':
                response.failure("返回msg【%s】与预期值success不符，返回报文【%s】，返回response headers为【%s】" % (json_resp["msg"], response.text, response.headers))
    except Exception as e:
        response.failure("返回状态码为【%s】，返回报文【%s】,返回response headers为【%s】" % (response.status_code, response.text, response.headers))

@events.test_start.add_listener
def on_test_start(**kwargs):
    print("A new test is starting")

@events.test_stop.add_listener
def on_test_stop(**kwargs):
    print("A new test is ending")

class Test1(HttpUser):
    weight = 1
    wait_time = between(0.0001, 0.0002)
    @task
    def apiinfo(self):
        #查询产品列表
        with self.client.get("/api/info", headers=headers, catch_response=True,timeout=60) as response:
            my_assert(response)
    def on_start(self):
        pass

# class Test2(HttpUser):
#     weight = 1
#     wait_time = between(0.0001, 0.0002)
#     @task
#     def productSearch(self):
#         # 商品搜索
#         # data = {"keyword":"白光"}
#         with self.client.get("/api/query/product/search?keyword=小绿瓶", headers=headers, catch_response=True,timeout=60) as response:
#             my_assert(response)
#     def on_start(self):
#         pass
#         # data = {"account":"18850369373","password":"x5HMgFqOWTp/s0sSE3QagQ==","channel":"W"}
#         # self.client.post("/api/user/general/login",json=data,
#         #                  headers=headers)
#
# class Test7(HttpUser):
#     weight = 1
#     wait_time = between(0.0001, 0.0002)
#     @task
#     def articlesSearch(self):
#         # 种草搜索
#         with self.client.get("/api/cms/articles/search?keyword=补水神器", headers=headers, catch_response=True,timeout=60) as response:
#             my_assert(response)
#     def on_start(self):
#         pass
#         # data = {"account":"18850369373","password":"x5HMgFqOWTp/s0sSE3QagQ==","channel":"W"}
#         # self.client.post("/api/user/general/login",json=data,
#         #                  headers=headers)
# class Test8(HttpUser):
#     weight = 1
#     wait_time = between(0.0001, 0.0002)
#     @task
#     def activitySearch(self):
#         # 活动搜索
#         with self.client.get("/api/activity/search?keyword=夏日&activityState=Y", headers=headers, catch_response=True,timeout=60) as response:
#             my_assert(response)
#     def on_start(self):
#         pass
#         # data = {"account":"18850369373","password":"x5HMgFqOWTp/s0sSE3QagQ==","channel":"W"}
#         # self.client.post("/api/user/general/login",json=data,
#         #                  headers=headers)
#
# class Test3(HttpUser):
#     weight = 1
#     wait_time = between(0.0001, 0.0002)
#     @task
#     def productDetail(self):
#         # 产品详情
#         with self.client.get("/api/query/product/1000000361", headers=headers, catch_response=True,timeout=60) as response:
#             my_assert(response)
#     def on_start(self):
#         pass
#         # data = {"account":"18850369373","password":"x5HMgFqOWTp/s0sSE3QagQ==","channel":"W"}
#         # self.client.post("/api/user/general/login",json=data,
#         #                  headers=headers)
#
# class Test4(HttpUser):
#     weight = 1
#     wait_time = between(0.0001, 0.0002)
#     @task
#     def activityList(self):
#         # 活动列表
#         with self.client.get("/api/activities?activityState=N", headers=headers, catch_response=True,timeout=60) as response:
#             my_assert(response)
#     def on_start(self):
#         pass
#         # data = {"account":"18850369373","password":"x5HMgFqOWTp/s0sSE3QagQ==","channel":"W"}
#         # self.client.post("/api/user/general/login",json=data,
#         #                  headers=headers)
#
# class Test5(HttpUser):
#     weight = 1
#     wait_time = between(0.0001, 0.0002)
#     @task
#     def publicNotice(self):
#         #公告列表查询
#         with self.client.get("/api/public-notice/query", headers=headers, catch_response=True,timeout=60) as response:
#             my_assert(response)
#     def on_start(self):
#         pass
#         # data = {"account":"18850369373","password":"x5HMgFqOWTp/s0sSE3QagQ==","channel":"W"}
#         # self.client.post("/api/user/general/login",json=data,
#         #                  headers=headers)
#
# class Test6(HttpUser):
#     weight = 1
#     wait_time = between(0.0001, 0.0002)
#     @task
#     def productCategories(self):
#         #分类列表查询
#         with self.client.get("/api/query/categories", headers=headers, catch_response=True,timeout=60) as response:
#             my_assert(response)
#     def on_start(self):
#         pass
#         # data = {"account":"18850369373","password":"x5HMgFqOWTp/s0sSE3QagQ==","channel":"W"}
#         # self.client.post("/api/user/general/login",json=data,
#         #                  headers=headers)


